package Eyjafjalla.save;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.exceptions.SaveFileLoadError;
import com.megacrit.cardcrawl.helpers.AsyncSaver;
import com.megacrit.cardcrawl.helpers.SaveHelper;
import com.megacrit.cardcrawl.saveAndContinue.SaveFileObfuscator;

import java.io.File;
import java.util.HashMap;

public class EyjafjallaSaveAndContinue {
    public static void saveEyjafjalla(EyjafjallaSave eyjafjallaSave){
        HashMap<Object,Object> params = new HashMap<>();
        params.put("enter_siesta",eyjafjallaSave.enter_siesta);
        params.put("face_pompeii",eyjafjallaSave.face_pompeii);
        params.put("enter_event",eyjafjallaSave.enter_event);
        params.put("reward_level",eyjafjallaSave.reward_level);
        params.put("event_percent",eyjafjallaSave.event_percent);

        Gson gson = (new GsonBuilder()).setPrettyPrinting().create();
        String data = gson.toJson(params);
        String filepath = getEyjafjallaSavePath(AbstractDungeon.player.chosenClass);
        if(Settings.isBeta){
            AsyncSaver.save(filepath + "BETA",data);
        }
        AsyncSaver.save(filepath, SaveFileObfuscator.encode(data,"key"));
    }

    public static EyjafjallaSave loadEyjafjalla(AbstractPlayer.PlayerClass c){
        String fileName = getEyjafjallaSavePath(c);

        try {
            return loadEyjafjalla(fileName);
        } catch (SaveFileLoadError var3) {
            return null;
        }
    }

    public static void deleteEyjafjalla(AbstractPlayer.PlayerClass c){
        String savePath = getEyjafjallaSavePath(c);
        Gdx.files.local(savePath).delete();
        Gdx.files.local(savePath + ".backUp").delete();
    }



    private static EyjafjallaSave loadEyjafjalla(String filePath) throws SaveFileLoadError{
        EyjafjallaSave saveFile = null;
        Gson gson = new Gson();
        String savestr = null;
        Exception err = null;

        try {
            savestr = loadSaveString(filePath);
            saveFile = gson.fromJson(savestr,EyjafjallaSave.class);
        } catch (Exception var6) {
            if (Gdx.files.local(filePath).exists()) {
                SaveHelper.preserveCorruptFile(filePath);
            }

            err = var6;
            if (!filePath.endsWith(".backUp")) {
                return loadEyjafjalla(filePath + ".backUp");
            }
        }

        if (saveFile == null) {
            throw new SaveFileLoadError("Didn't Find Right Now: " + filePath, err);
        } else {
            return saveFile;
        }
    }

    private static String loadSaveString(String filePath) {
        FileHandle file = Gdx.files.local(filePath);
        String data = file.readString();
        return SaveFileObfuscator.isObfuscated(data) ? SaveFileObfuscator.decode(data, "key") : data;
    }

    private static String getEyjafjallaSavePath(AbstractPlayer.PlayerClass c){
        StringBuilder sb = new StringBuilder();
        sb.append("saves" + File.separator);
        if (CardCrawlGame.saveSlot != 0) {
            sb.append(CardCrawlGame.saveSlot).append("_");
        }

        sb.append(c.name()).append("_eyjafjalla.autosave");
        return sb.toString();
    }
}
